/**
 * Copyright (C) 2025 pony working team
 * SPDX-License-Identifier: MulanPSL-2.0
 */

/* define to prevent recursive inclusion -------------------------------------*/
#ifndef __PONY_YMODEM_H__
#define __PONY_YMODEM_H__

/* includes (standard library, system) ---------------------------------------*/
#include <stdint.h>
#include <stddef.h>
/* includes (other library) --------------------------------------------------*/
/* includes (project) --------------------------------------------------------*/
/* includes (local) ----------------------------------------------------------*/

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

/* defines -------------------------------------------------------------------*/

#define YMODEM_SUCCESS                    0    // Successful transfer
#define YMODEM_ERROR_INVAL                -1   // Null modem pointer
#define YMODEM_ERROR_PART_GET_SIZE        -2   // Failed to get partition size
#define YMODEM_ERROR_PART_ERASE           -3   // Failed to erase to partition
#define YMODEM_ERROR_PART_WRITE           -4   // Failed to write to partition
#define YMODEM_ERROR_PART_NO_ENOUGH_SPACE -5   // Not enough space in partition
#define YMODEM_ERROR_ABORTED_BY_SENDER    -6   // Transfer aborted by sender
#define YMODEM_ERROR_ABORTED_BY_USER      -7   // Transfer aborted by sender
#define YMODEM_ERROR_TOO_MANY_ERRORS      -8   // Too many transmission errors
#define YMODEM_ERROR_INVALID_FILE         -9   // Invalid file size or name

/* typedefs ------------------------------------------------------------------*/

typedef struct ymodem {
    uint32_t part_id; /* write logical partition id */
    int (*putchar)(int c);
    int (*getchar)(size_t timeout);
} ymodem_t;

/* macro ---------------------------------------------------------------------*/
/* inlines -------------------------------------------------------------------*/
/* externs -------------------------------------------------------------------*/

extern int ymodem_receive(ymodem_t *modem);
extern size_t ymodem_get_filesize(void);
extern const char *ymodem_get_filename(void);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* __PONY_YMODEM_H__ */
